/* Copyright (c) 2015-present, salesforce.com, inc. All rights reserved */
/* Licensed under BSD 3-Clause - see LICENSE.txt or git.io/sfdc-license */

// # Global Header Button Component

// ## Dependencies

// ### React
import React from 'react';
import PropTypes from 'prop-types';

import assign from 'lodash.assign';
import classnames from 'classnames';

import checkProps from './check-props';

import Button from '../button';
import Popover from '../popover';

// ## Constants
import { GLOBAL_HEADER_PROFILE } from '../../utilities/constants';

/**
 * This component is an `Avatar` component that opens a `Popover` component when clicked.
 */
class GlobalHeaderProfile extends React.Component {
	constructor(props) {
		super(props);
		checkProps(GLOBAL_HEADER_PROFILE, props);
	}

	render() {
		const buttonAriaProps = {
			'aria-haspopup': true,
		};
		const popoverProps = assign(
			{
				align: 'bottom right',
				body: <span />,
				className: this.props.className,
				heading: this.props.userName,
				id: this.props.id,
				triggerClassName: 'slds-dropdown-trigger slds-dropdown-trigger_click',
			},
			this.props.popover ? this.props.popover.props : {}
		);
		// Note: the default avatar is done this way to prevent documentation from showing this giant string
		/* eslint-disable max-len */
		let avatar =
			'';
		/* eslint-enable max-len */

		// eslint-disable-next-line fp/no-delete
		delete popoverProps.children;

		avatar = this.props.avatar ? this.props.avatar : avatar;

		return (
			<Popover {...popoverProps}>
				<Button
					className={classnames(
						'slds-global-actions__avatar slds-global-actions__item-action',
						this.props.buttonClassName
					)}
					title={this.props.userName}
					variant="icon"
					{...buttonAriaProps}
				>
					{typeof avatar === 'string' ? (
						<span className="slds-avatar slds-avatar_circle slds-avatar_medium">
							<img alt={this.props.userName} src={avatar} title="User avatar" />
						</span>
					) : (
						avatar || null
					)}
				</Button>
			</Popover>
		);
	}
}

// ### Display Name
GlobalHeaderProfile.displayName = GLOBAL_HEADER_PROFILE;

// ### Prop Types
GlobalHeaderProfile.propTypes = {
	/**
	 * Extra classnames to apply to the popover.
	 */
	className: PropTypes.string,
	/**
	 * An image URL or avatar node to display for the user profile. Defaults to a base64 encoded generic user avatar image string
	 */
	avatar: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
	/**
	 * CSS classes to be added to the `button` element.
	 */
	buttonClassName: PropTypes.oneOfType([
		PropTypes.array,
		PropTypes.object,
		PropTypes.string,
	]),
	/**
	 * A unique ID is needed in order to support keyboard navigation, ARIA support, and connect the popover to the triggering button. If an id is not provided, it will be automatically generated.
	 */
	id: PropTypes.string,
	/**
	 * A `Popover` component. The props from this popover will be merged and override any default props.
	 */
	popover: PropTypes.node,
	/**
	 * The user name of the profile. Defaults to "User Name"
	 */
	userName: PropTypes.string,
};

// ### Default Props
GlobalHeaderProfile.defaultProps = {
	userName: 'User Name',
};

export default GlobalHeaderProfile;
